home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / TinyMeter / Source / TinyMeter_main / main.c < prev    next >
C/C++ Source or Header  |  1997-02-27  |  9KB  |  437 lines

  1. #include <libraries/commodities.h>
  2. #include <proto/SysInfo.h>
  3. #include <libraries/SysInfo.h>
  4. #include <libraries/ScreenNotify.h>
  5. #include <exec/memory.h>
  6. #include <dos/dos.h>
  7. #include <intuition/intuition.h>
  8. #include <intuition/classusr.h>
  9. #include <intuition/gadgetclass.h>
  10. #include <intuition/cghooks.h>
  11. #include <intuition/icclass.h>
  12. #include <intuition/classes.h>
  13. #include <utility/tagitem.h>
  14. #include "gaugeclass.h"
  15. #include "launchclass.h"
  16. #include "tinymeter.h"
  17. /* defaultshit */
  18. #define Version "4.31"
  19.  
  20. #ifndef MAKE_ID
  21. #define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  22. #endif                                                                       
  23.  
  24. static const char *version="$VER: TinyMeter V" Version;
  25.  
  26. struct Library  *RetinaBase;
  27. struct Library  *ShutdownBase;
  28. struct Library  *WBStartBase;
  29. struct Library  *TimerBase;
  30. struct Library  *SysInfoBase;
  31. struct Library  *ScreenNotifyBase;
  32. struct Library  *CxBase;
  33. struct Library  *DataTypesBase;
  34. struct Library  *LocaleBase;
  35.  
  36. struct SysInfo  *si;
  37. ULONG  executive;
  38.  
  39. struct WBStartup *_WBBenchMsg;
  40. ULONG *TM_Pool; /* Global memory pool */
  41.  
  42. #include <clib/wbstart_protos.h>
  43. extern struct Library *WBStartBase;
  44. #include <pragmas/wbstart_pragmas.h>
  45.  
  46. LONG WBStartTags(Tag tag1, ...)
  47. {
  48.  return(WBStartTagList((struct TagItem *) &tag1));
  49. }
  50.  
  51. __saveds ULONG *pAllocVec(ULONG memSize)
  52. {
  53.     ULONG *memory; memSize += sizeof(ULONG);
  54.     if(memory=(ULONG *)AllocPooled(TM_Pool,memSize))
  55.     {
  56.     memory[0]=memSize;
  57.     memory+=1L;
  58.     }
  59.     return(memory);
  60. }
  61.  
  62. __saveds void pFreeVec(ULONG *memory)
  63. {
  64.     if(memory)
  65.     {
  66.     memory-=1L;
  67.     FreePooled(TM_Pool,memory,memory[0]);
  68.     }
  69. }
  70.  
  71. void show(int what)
  72. {
  73.     static struct EasyStruct easymsg =
  74.     {
  75.     sizeof(struct EasyStruct),
  76.     0,
  77.     "TinyMeter " Version " information",
  78.     0L,
  79.     "OK"
  80.     };
  81.     static char *err_msg[]=
  82.     {
  83.     "Not enough memory.",
  84.     "Could not open the window.",
  85.     "Port initialization failed",
  86.     "BOOPSI allocation failed",
  87.     "timer.device error!",
  88.     "Startup problem!",
  89.     "Preferences error. Using defaults!",
  90.     "Could not open listfile"
  91.     };
  92.  
  93.     easymsg.es_TextFormat=err_msg[what];
  94.     EasyRequest(NULL, &easymsg, NULL);
  95. }
  96.  
  97. int getNum(struct tm_gau_set *list)
  98. {
  99.     int num=1; while(list=list->next)num++; return(num);
  100. }
  101.  
  102. void useDefault(struct tm_sys_set *set,struct tm_data *data)
  103. {
  104.     static struct tm_sys_set default_set=
  105.     {
  106.     0x544D3430,
  107.     1,
  108.  
  109.     10,
  110.     10,
  111.     450,
  112.     2,
  113.  
  114.     "Workbench",
  115.  
  116.     "",
  117.     8,
  118.  
  119.     bg_none,
  120.     "",
  121.     FALSE,
  122.     -3,
  123.     FALSE,0x55555555,0x55555555,0x55555555,
  124.  
  125.     5,
  126.     1,
  127.  
  128.     0,
  129.     TRUE,
  130.  
  131.     win_normal,
  132.     0,
  133.     4,
  134.     4,
  135.     8,
  136.     8,
  137.  
  138.     bd_standard,
  139.     FALSE,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,
  140.     FALSE,0x0,0x0,0x0,
  141.  
  142.     idle_own
  143.     };
  144.  
  145.     static struct tm_gau_set g3=
  146.     {
  147.     typ_idle,
  148.     typ_histmeter,
  149.  
  150.     200,
  151.  
  152.     "",
  153.     8,
  154.  
  155.     "ChipMem",
  156.     "CPU %p%% free",
  157.     "",
  158.     ind_right,
  159.     TRUE,
  160.     TRUE,
  161.     TRUE,
  162.     TRUE,
  163.     FALSE,
  164.     FALSE,
  165.     TRUE,
  166.  
  167.     TRUE,2,0,0,
  168.     TRUE,2,0,0,
  169.     FALSE,0,0xFFFFFFFF,0,
  170.     FALSE,0,0,0xFFFFFFFF,
  171.     FALSE,0xFFFFFFFF,0,0,
  172.     TRUE,2,0,0,
  173.     TRUE,1,0,0,
  174.     TRUE,1,0,0,
  175.  
  176.     NULL
  177.     };
  178.  
  179.     static struct tm_gau_set g2=
  180.     {
  181.     typ_fast,
  182.     typ_gauge,
  183.  
  184.     20,
  185.  
  186.     "",
  187.     8,
  188.  
  189.     "FastMem",
  190.     "%td Bytes free",
  191.     "",
  192.     ind_right,
  193.     TRUE,
  194.     TRUE,
  195.     TRUE,
  196.     TRUE,
  197.     FALSE,
  198.     FALSE,
  199.     FALSE,
  200.  
  201.     TRUE,2,0,0,
  202.     TRUE,2,0,0,
  203.     FALSE,0,0xFFFFFFFF,0,
  204.     FALSE,0,0,0xFFFFFFFF,
  205.     FALSE,0xFFFFFFFF,0,0,
  206.     TRUE,2,0,0,
  207.     TRUE,1,0,0,
  208.     TRUE,0,0,0,
  209.  
  210.     &g3
  211.     };
  212.  
  213.     static struct tm_gau_set g1=
  214.     {
  215.     typ_chip,
  216.     typ_gauge,
  217.  
  218.     20,
  219.  
  220.     "topaz.font",
  221.     8,
  222.  
  223.     "ChipMem",
  224.     "%td Bytes free",
  225.     "",
  226.     ind_right,
  227.     TRUE,
  228.     TRUE,
  229.     TRUE,
  230.     TRUE,
  231.     FALSE,
  232.     FALSE,
  233.     FALSE,
  234.  
  235.     TRUE,2,0,0,
  236.     TRUE,2,0,0,
  237.     FALSE,0,0xFFFFFFFF,0,
  238.     FALSE,0,0,0xFFFFFFFF,
  239.     FALSE,0xFFFFFFFF,0,0,
  240.     TRUE,2,0,0,
  241.     TRUE,1,0,0,
  242.     TRUE,0,0,0,
  243.  
  244.     &g2
  245.     };
  246.  
  247.     struct tm_gau_set   *many,
  248.             *act,
  249.             *old;
  250.  
  251.     CopyMem((char *)&default_set,set,sizeof(struct tm_sys_set));
  252.     if(act=(struct tm_gau_set *)pAllocVec(sizeof(struct tm_gau_set)))
  253.     {
  254.     many=(struct tm_gau_set *)&g1; data->list=act; CopyMem((char *)many,act,sizeof(struct tm_gau_set));
  255.     while(many=many->next)
  256.     {
  257.         old=act;
  258.         if(act=(struct tm_gau_set *)pAllocVec(sizeof(struct tm_gau_set)))
  259.            CopyMem((char *)many,old->next=act,sizeof(struct tm_gau_set));
  260.     }
  261.     act->next=0L;
  262.     }
  263.     else data->list=0L;
  264. }
  265.  
  266. int main(void)
  267. {
  268.  
  269.     struct tm_sys_set   *set;
  270.     struct tm_data      *data;
  271.  
  272.     Class               *gclass,*lclass;
  273.     ULONG                cxsigflag;
  274.     CxMsg               *msg;
  275.     CxObj               *broker;
  276.     struct MsgPort      *broker_mp;
  277.     ULONG               broker_err;
  278.  
  279.     struct NewBroker newbroker =
  280.     {
  281.     NB_VERSION,
  282.     "TinyMeter V" Version,
  283.     "TinyMeter V" Version " © by Tinic Urou",
  284.     "Shows system states",
  285.     NBU_UNIQUE,
  286.     0,
  287.     -3,
  288.     0,
  289.     0
  290.     };
  291.  
  292.     if(LocaleBase=(struct Library *)OpenLibrary("locale.library",0L))
  293.     {
  294.     if(WBStartBase=(struct Library *)OpenLibrary("wbstart.library",0L))
  295.     {
  296.         if(DataTypesBase=(struct Library *)OpenLibrary("datatypes.library",0L))
  297.         {        
  298.         if(CxBase=(struct Library *)OpenLibrary("commodities.library",0L))
  299.         {
  300.             if(TM_Pool=(ULONG *)CreatePool(MEMF_PUBLIC|MEMF_CLEAR,32768L,16384L))
  301.             {
  302.             if(data=(struct tm_data *)pAllocVec(sizeof(struct tm_data)))
  303.             {
  304.                 if(set=(struct tm_sys_set *)pAllocVec(sizeof(struct tm_sys_set)))
  305.                 {                                   
  306.                 if((gclass=(Class *)initGaugeGadgetClass())&&(lclass=(Class *)initLauncherGadgetClass()))
  307.                 {
  308.                     BPTR   my_file;
  309.                     struct tm_gau_set *many,*act;
  310.                     if(my_file=(BPTR)Open("ENV:TinyMeter",MODE_OLDFILE))
  311.                     {
  312.                     if(!Read(my_file,set,(ULONG)sizeof(struct tm_sys_set)))
  313.                     {
  314.                         show(err_nopre);
  315.                         useDefault(set,data);
  316.                     }
  317.                     else
  318.                     {
  319.                         if(set->set_header==0x544D3430)
  320.                         {
  321.                         if(data->list=act=(struct tm_gau_set *)pAllocVec(sizeof(struct tm_gau_set)+2L))
  322.                         {
  323.                             while(Read(my_file,(char *)act,(ULONG)sizeof(struct tm_gau_set))&&act)
  324.                             {
  325.                             many=act;
  326.                             if(act=(struct tm_gau_set *)pAllocVec(sizeof(struct tm_gau_set))) many->next=act;
  327.                             }
  328.                             many->next=NULL; pFreeVec((ULONG *)act);
  329.                         }
  330.                         }
  331.                         else
  332.                         {
  333.                         show(err_nopre);
  334.                         useDefault(set,data);
  335.                         }
  336.                     }
  337.                     Close(my_file);
  338.                     }
  339.                     else
  340.                     {
  341.                     show(err_nopre);
  342.                     useDefault(set,data);
  343.                     }
  344.                     data->num_of_gaug=getNum(data->list);
  345.                     SetTaskPri(FindTask(0L),(LONG)set->pri);
  346.                     Delay(set->start_wait*50L);
  347.                     if(broker_mp = (struct MsgPort *)CreatePort(0,0))
  348.                     {
  349.                     newbroker.nb_Port = (struct MsgPort *)broker_mp;
  350.                     if (broker = (CxObj *)CxBroker(&newbroker,(LONG *)&broker_err))
  351.                     {
  352.                         cxsigflag = 1L << broker_mp->mp_SigBit;
  353.                         ActivateCxObj(broker, 1L);
  354.  
  355.                         RetinaBase=(struct Library *)OpenLibrary("retina.library",0L);
  356.  
  357.                         switch (set->Executive)
  358.                         {
  359.                         case    idle_none:
  360.                             executive=idle_none;
  361.                             break;
  362.                         case    idle_executive:
  363.                             if(SysInfoBase = (struct Library *)OpenLibrary(SYSINFONAME, SYSINFOVERSION))
  364.                             {
  365.                                 if(si =(struct SysInfo *)InitSysInfo())
  366.                                     executive=idle_executive;
  367.                                 else
  368.                                     executive=idle_none;
  369.                             }
  370.                             else executive=idle_none;
  371.                             break;
  372.                         case    idle_own:
  373.                             init_idle();
  374.                             executive=idle_own;
  375.                             break;
  376.                         }
  377.  
  378.                         data->scrnot=FALSE; if(set->start_usescreennotify)
  379.                         if(ScreenNotifyBase=(struct Library *)OpenLibrary("screennotify.library",SCREENNOTIFY_VERSION))
  380.                             data->scrnot=TRUE;
  381.  
  382.                         if(InitWorkbenchPath(data))
  383.                         {
  384.                         if(openWindow(set,data,gclass,lclass))
  385.                         {
  386.                             set=(struct tm_sys_set *)handler(set,data,broker_mp,broker,cxsigflag,gclass,lclass);
  387.                             closeWindow(set,data);
  388.                         }
  389.                         else show(err_nowin);
  390.                         FreeWorkbenchPath(data);
  391.                         }
  392.                         else show(err_nosta);
  393.                         switch (executive)
  394.                         {
  395.                         case    idle_executive:
  396.                             FreeSysInfo(si);
  397.                             CloseLibrary(SysInfoBase);
  398.                             break;
  399.                         case    idle_own:
  400.                             free_idle();
  401.                             break;
  402.  
  403.                         }
  404.                         if(data->scrnot)CloseLibrary(ScreenNotifyBase);
  405.                         if(RetinaBase)  CloseLibrary(RetinaBase);
  406.                         while(msg = (CxMsg *)GetMsg(broker_mp)) ReplyMsg((struct Message *)msg);
  407.                         DeleteCxObjAll(broker);
  408.                     }
  409.                     else show(err_nosta);
  410.                     DeletePort(broker_mp);
  411.                     }
  412.                     else show(err_nosta);
  413.                     freeGaugeGadgetClass(gclass);
  414.                     freeLauncherGadgetClass(lclass);
  415.                 }
  416.                 else show(err_noboo);
  417.                 }
  418.                 else show(err_nomem);
  419.             }
  420.             else show(err_nomem);
  421.             DeletePool(TM_Pool); /* also deletes our allocated mem */
  422.             }
  423.             CloseLibrary(CxBase);
  424.         }
  425.         CloseLibrary(DataTypesBase);
  426.         }
  427.         CloseLibrary(WBStartBase);
  428.     }
  429.     CloseLibrary(LocaleBase);
  430.     }
  431. }
  432.  
  433. int wbmain(void)
  434. {
  435.     return(main());
  436. }
  437.